package display3d.away3d {
	import jazztures.utils.GeometryUtils;
	import away3d.arcane;
	import away3d.core.math.Vector3DUtils;
	import flash.geom.Vector3D;
	import away3d.core.base.Object3D;
	import away3d.containers.ObjectContainer3D;
	import away3d.core.utils.Cast;
	import away3d.loaders.Collada;
	import away3d.materials.BitmapMaterial;

	import jazztures.objects.Skeleton;

	import flash.geom.Matrix3D;

	public class Mario3D extends Doll3D
	{
		[Embed(source="../assets/mario_model.dae",mimeType="application/octet-stream")]
		private var MarioModel:Class;
			
		[Embed(source="../assets/mario_texture.jpg")]
		private var MarioTexture:Class;
		
		protected var model:ObjectContainer3D;                                                                                                                                                                                      
		
		public function Mario3D()
		{
			super();
		}
		
		override protected function build():void{
			var collada:Collada = new Collada();
			model = collada.parseGeometry(new MarioModel()) as ObjectContainer3D;		
						
			model.scale(30);						
			addChild(model);
			
			setMaterial();

			head3D = model.getBoneByName("head");
			neck3D = model.getBoneByName("neck");
			torso3D = model.getBoneByName("torso");
			
			leftShoulder3D = model.getBoneByName("leftShoulder");
			leftElbow3D = model.getBoneByName("leftElbow");
			
			rightShoulder3D = model.getBoneByName("rightShoulder");
			rightElbow3D = model.getBoneByName("rightElbow");
			
			leftHip3D = model.getBoneByName("leftHip");
			leftKnee3D = model.getBoneByName("leftKnee");
			
			rightHip3D = model.getBoneByName("rightHip");
			rightKnee3D = model.getBoneByName("rightKnee");
		}
		
		protected function setMaterial():void{
			model.materialLibrary.getMaterial("FF_FF_FF_mario1").material = new BitmapMaterial(Cast.bitmap(MarioTexture));
		}
		
		override public function set skeleton(skeleton:Skeleton):void{
			super.skeleton = skeleton;
		}
		
		override public function update():void{
			setPosition(neck3D, neck.vector3D);
			setRelativeRotation(neck3D, neck.matrix3D);


			var headRotation:Matrix3D = head.matrix3D.clone();
			var invNeck:Matrix3D = neck.matrix3D.clone();
			invNeck.invert();
			headRotation.append(invNeck);
			setRelativeRotation(head3D, headRotation);
			
			var leftShoulderRotation:Matrix3D = leftShoulder.matrix3D.clone();
			leftShoulderRotation.append(invNeck);
			setRelativeRotation(leftShoulder3D, leftShoulderRotation);
			
			var leftElbowRotation:Matrix3D = leftElbow.matrix3D.clone();
			var invLeftShoulder:Matrix3D = leftShoulder.matrix3D.clone();
			invLeftShoulder.invert();
			leftElbowRotation.append(invLeftShoulder);
			setRelativeRotation(leftElbow3D, leftElbowRotation);
			
			var rightShoulderRotation:Matrix3D = rightShoulder.matrix3D.clone();
			rightShoulderRotation.append(invNeck);
			setRelativeRotation(rightShoulder3D, rightShoulderRotation);
			
			var rightElbowRotation:Matrix3D = rightElbow.matrix3D.clone();
			var invRightShoulder:Matrix3D = rightShoulder.matrix3D.clone();
			invRightShoulder.invert();
			rightElbowRotation.append(invRightShoulder);
			setRelativeRotation(rightElbow3D, rightElbowRotation);
			
			var torsoRotation:Matrix3D = torso.matrix3D.clone();
			torsoRotation.append(invNeck);
			setRelativeRotation(torso3D, torsoRotation);
			
			var invTorso:Matrix3D = torso.matrix3D.clone();
			invTorso.invert();
			
			var leftHipRotation:Matrix3D = leftHip.matrix3D.clone();
			leftHipRotation.append(invTorso);
			setRelativeRotation(leftHip3D, leftHipRotation);
			
			var invLeftHip:Matrix3D = leftHip.matrix3D.clone();
			invLeftHip.invert();
			
			var leftKneeRotation:Matrix3D = leftKnee.matrix3D.clone();
			leftKneeRotation.append(invLeftHip);
			setRelativeRotation(leftKnee3D, leftKneeRotation);
			
			var rightHipRotation:Matrix3D = rightHip.matrix3D.clone();
			rightHipRotation.append(invTorso);
			setRelativeRotation(rightHip3D, rightHipRotation);
			
			var invRightHip:Matrix3D = rightHip.matrix3D.clone();
			invRightHip.invert();
			
			var rightKneeRotation:Matrix3D = rightKnee.matrix3D.clone();
			rightKneeRotation.append(invRightHip);
			setRelativeRotation(rightKnee3D, rightKneeRotation);
		}
		
		private function setRelativeRotation(object:Object3D, mat:Matrix3D):void{
			mat = mat.clone();
			mat.invert();
			mat.position = object.transform.position;
			object.transform = mat;			
		}
	}
}